const url = require("url");
const express = require("express");
const router = express();
const bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true, limit: '500mb' }));
router.use(bodyParser.json({ limit: '500mb' }));
const db = require("../db/db.js");
const JWT = require("../middleware/jwt.js");
//登录操作--------------------------------------------------
router.post("/login", (req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*')
  db.login("user", req, res);
});
// 验证token是否有效
router.post("/identification", JWT, async (req, res) => {
  res.json({
    code: 200,
    message: "token有效",
  });
});
//修改密码--------------------------------------------------
router.post("/user/updatepwd", JWT, (req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  db.updatepwd("user", req, res);
});
//获取用户信息------------------------------------
router.get("/getUser", (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  db.getUser('user',req, res);
});
//获取按钮信息------------------------------------
router.get("/getButton", (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  db.getButton(req, res);
});
//获取app日志------------------------------------
router.post("/getAppLog", (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  db.getAppLog(req, res);
});
//获取app状态------------------------------------
router.post("/getAppStatus", (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  db.getAppStatus(req, res);
});
// 开启APP
router.post("/startApp", (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  db.startApp(req, res);
});
// 停止APP
router.post("/stopApp", (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  db.stopApp(req, res);
});
//测试------------------------------------
router.get("/test", (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  db.test(req, res);
});
// 获取配置信息 
router.get("/getConfig", (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  db.getConfig(req, res);
});
// 更新配置信息
router.post("/updateConfig", JWT, (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  db.updateConfig(req, res);
});

    module.exports = router